﻿using ZYCommon;
using System;
using System.Collections;
using System.Reflection;
using System.Text;
using System.Xml;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    

    public class UnderWriteMarkCollection : IXMLSerializable, IEnumerable
    {
        public bool Enable = true;
        private ArrayList myItems = new ArrayList();
        public UnderWriteMark NewMark = null;
        public ZYTextDocument OwnerDocument;
        public ZYTextElement OwnerElement;

        public UnderWriteMark AddMark(string strUserName, string strSenior)
        {
            if ((this.myItems.Count == 0) || (this.LastMark.Senior == strUserName))
            {
                this.NewMark = new UnderWriteMark();
                this.NewMark.UserName = strUserName;
                this.NewMark.Senior = strSenior;
                this.NewMark.MarkTime = DateTime.Now;
                this.myItems.Add(this.NewMark);
                if (this.OwnerDocument != null)
                {
                    this.NewMark.SaveLogIndex = this.OwnerDocument.SaveLogs.CurrentIndex;
                    this.OwnerDocument.UpdateUserName();
                }
                return this.NewMark;
            }
            return null;
        }

        public bool CanMark(string strUserName)
        {
            if (this.myItems.Count > 0)
            {
                return (((UnderWriteMark) this.myItems[this.myItems.Count - 1]).Senior == strUserName);
            }
            return true;
        }

        public void Clear()
        {
            this.myItems.Clear();
        }

        public bool FromXML(XmlElement RootElement)
        {
            this.Clear();
            if (RootElement != null)
            {
                foreach (XmlNode node in RootElement.ChildNodes)
                {
                    if (node is XmlElement)
                    {
                        UnderWriteMark mark = new UnderWriteMark();
                        mark.FromXML(node as XmlElement);
                        this.myItems.Add(mark);
                    }
                }
                return true;
            }
            return false;
        }

        public string GetDisplayText()
        {
            StringBuilder builder = new StringBuilder();
            foreach (UnderWriteMark mark in this.myItems)
            {
                if (builder.Length > 0)
                {
                    builder.Append("\r\n");
                }
                builder.Append(mark.UserName);
                builder.Append(" 签名于 ");
                builder.Append(mark.MarkTime.ToString("yyyy年MM月dd日 HH:mm:ss"));
            }
            if ((this.myItems.Count > 0) && !StringCommon.isBlankString(this.LastMark.Senior))
            {
                builder.Append("\r\n 本区域指定由[ " + this.LastMark.Senior + " ]签名");
            }
            return builder.ToString();
        }

        public IEnumerator GetEnumerator()
        {
            return new myEnumerator(this.myItems);
        }

        public string GetXMLName()
        {
            return "underwritemarks";
        }

        public bool IndexAvailable(int Index)
        {
            return ((Index >= 0) && (Index < this.myItems.Count));
        }

        public string[] ToStringArray()
        {
            string[] strArray = new string[this.myItems.Count];
            for (int i = 0; i < this.myItems.Count; i++)
            {
                strArray[i] = ((UnderWriteMark) this.myItems[i]).DisplayText();
            }
            return strArray;
        }

        public bool ToXML(XmlElement RootElement)
        {
            if (RootElement != null)
            {
                foreach (UnderWriteMark mark in this.myItems)
                {
                    XmlElement newChild = RootElement.OwnerDocument.CreateElement(mark.GetXMLName());
                    RootElement.AppendChild(newChild);
                    mark.ToXML(newChild);
                }
                return true;
            }
            return false;
        }

        public int Count
        {
            get
            {
                return this.myItems.Count;
            }
        }

        public UnderWriteMark this[int index]
        {
            get
            {
                return (UnderWriteMark) this.myItems[index];
            }
        }

        public UnderWriteMark LastMark
        {
            get
            {
                return (UnderWriteMark) this.myItems[this.myItems.Count - 1];
            }
        }

        public string LastSenior
        {
            get
            {
                if (this.myItems.Count > 0)
                {
                    return ((UnderWriteMark) this.myItems[this.myItems.Count - 1]).Senior;
                }
                return null;
            }
        }

        private class myEnumerator : IEnumerator
        {
            private int index;
            private ArrayList myList;

            public myEnumerator(ArrayList lst)
            {
                this.myList = lst;
                this.index = -1;
            }

            public bool MoveNext()
            {
                this.index++;
                return (this.index < this.myList.Count);
            }

            public void Reset()
            {
                this.index = -1;
            }

            public object Current
            {
                get
                {
                    return (UnderWriteMark) this.myList[this.index];
                }
            }
        }
    }
}

